package com.atguigu.dga.meta.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.dga.meta.bean.TableMetaInfo;
import com.atguigu.dga.meta.bean.TableMetaInfoExtra;
import com.atguigu.dga.meta.bean.TableMetaInfoQuery;
import com.atguigu.dga.meta.bean.TableMetaInfoVO;
import com.atguigu.dga.meta.service.TableMetaInfoExtraService;
import com.atguigu.dga.meta.service.TableMetaInfoService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 元数据表 前端控制器
 * </p>
 *
 * @author zhangchen
 * @since 2024-07-24
 */
@RestController
@RequestMapping("/tableMetaInfo")
public class TableMetaInfoController {


    @Autowired
    TableMetaInfoService tableMetaInfoService;

    @Autowired
    TableMetaInfoExtraService tableMetaInfoExtraService;

    @GetMapping("/table-list")
    public String getTableList(TableMetaInfoQuery tableMetaInfoQuery ){
        List<TableMetaInfoVO> list = tableMetaInfoService.getListForQuery(tableMetaInfoQuery);

        Integer total = tableMetaInfoService.getTotalForQuery(tableMetaInfoQuery);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("list",list);
        resultMap.put("total",total);

        return JSON.toJSONString(resultMap)  ;

    }

    @GetMapping("/table/{id}")
    public String getTableById(@PathVariable("id") Long id){
        TableMetaInfo tableMetaInfo = tableMetaInfoService.getById(id);

        QueryWrapper<TableMetaInfoExtra> queryWrapper = new QueryWrapper<TableMetaInfoExtra>()
                .eq("table_name", tableMetaInfo.getTableName())
                .eq("schema_name", tableMetaInfo.getSchemaName());
        TableMetaInfoExtra tableMetaInfoExtra = tableMetaInfoExtraService.getOne(queryWrapper);

        tableMetaInfo.setTableMetaInfoExtra(tableMetaInfoExtra);
        return JSON.toJSONString(tableMetaInfo);
    }


    @PostMapping("/tableExtra")
    public String saveTableExtra(@RequestBody TableMetaInfoExtra tableMetaInfoExtra){
        tableMetaInfoExtra.setUpdateTime(new Date());
        tableMetaInfoExtraService.saveOrUpdate(tableMetaInfoExtra);
        return "success!!!!";
    }

}
